<a class="mini-button" style="${style!}" iconCls="icon-node" id="${id!}" onclick="downLoadGridData('${theGridId!}','${theFileName!}')">文件下载</a>

<script type="text/javascript" charset="utf-8">
    let _gMessageId;
    let _gInterval;
    let _grid;
    let _gridId = "";
    let time = "";
    let downloadStatus = "";

    /**
     * 下载方法
     * @param gridId 需要下载表格的ID
     * @param fileName 下载后存储的文件名
     */
    function downLoadGridData(gridId, fileName) {
        time = new Date().getTime();

        _gridId = gridId;
        _grid = mini.get(gridId);

        if(!_gridId){
            public_showErrorTips("未找到表格对象，请与管理员联系。");
            return;
        }

        if(!_grid.totalCount || _grid.totalCount <= 0){
            public_showErrorTips("表格无数据，无法导出。");
            return;
        }

        // 创建下载等待对话框
        _gMessageId = mini.loading("下载准备中，请稍后....");


        // 创建form表单，开始正式的下载
        downloadGridData2(_gridId, fileName);


    }

    function downloadGridData2(gridId, fileName) {
        let grid = mini.get(gridId);
        let params = grid.getLoadParams();
        let url = grid.getUrl();

        //将params列表里面的请求参数去除掉分页参数以后格式化成form提交所需要的格式
        let paramsStr = "";
        for (let i in params) {
            if (i != "pageIndex" && i != "pageSize" && i != "sortField"
                && i != "sortOrder") {
                if(!public_isEmpty(params[i])) {
                    paramsStr += i + "=" + params[i] + "&&";
                }
            }
        }
        //去除最后2个&&
        if(paramsStr.length > 2) {
            paramsStr = paramsStr.substring(0, paramsStr.length - 2);
        }

        let columnList = mini.encode(download_grid_getColumnInfo(grid));

        $.post("${base}/download/createExcel",{sys_url:url,sys_fileName:fileName,sys_params:paramsStr,columnList:columnList,_id:_gridId+time},function(result){

            if(result.ok="true"){
                // 发送一个确定的请求
                location.href="${base}/download/exportExcel?czyId=" + result.message + "&fileName=" + fileName;
            }
            else{
                public_showErrorTips(result.message);
            }

            mini.hideMessageBox(_gMessageId);
        });

    }

    /**
     * 获取grid列标题
     * @param grid
     */
    function download_grid_getColumnInfo(grid) {
        let columns = grid.getColumns();
        let columnArray = [];
        for (let i = 0; i < columns.length; i++) {
            let column = columns[i];
            download_grid_getColumnInfoRecursive(column,columnArray);//递归获取
        }
        return columnArray;
    }

    // 递归获取表格里的抬头信息（因有复合抬头，所以需要递归处理）
    function download_grid_getColumnInfoRecursive(column,columnArray) {
        if(column.columns) { //有子节点
            for (let i = 0; i < column.columns.length; i++) {
                let tempColumn = column.columns[i];
                download_grid_getColumnInfoRecursive(tempColumn,columnArray);
            }
        } else {
            // 无子节点
            // 获取column里面的titile,field,以及用于翻译的cacheName
            if (!public_isEmpty(column.header) && !public_isEmpty(column.field)) {
                let obj = {};
                obj.header = column.header;
                obj.field = column.field;
                if (!public_isEmpty(column.cacheName)) {
                    obj.cacheName = column.cacheName;
                }
                columnArray.push(obj);
            }
        }
    }

</script>